﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;

/*
#################################################################################################################
#                                                                                                               #
#                                                Auto Auth-Code 说明                                            #
#                                    作者：陈粤雄  联系：Aresh@163.com 时间:2009-05-11                          #
#               欢迎使用本控件，你可以免费自由使用及转载本控件及所有源代码，但请保留本说明信息                  #
#                                                                                                               #
#################################################################################################################
*/
namespace HAOest.Web.UI.WebControls.AutoAuthCode
{
    internal class BitmapProvide
    {
        /// <summary>
        /// 按ImageType获取图像
        /// </summary>
        /// <param name="imageType">图像类型</param>
        /// <param name="codeText">文本</param>
        /// <param name="imgStyle">图像样式</param>
        /// <returns></returns>
        internal static Bitmap GetBitmap(ImageTypes imageType, string codeText, ImageStyle imgStyle)
        {
            Font font = new Font("Verdana", imgStyle.TextFontSize, (FontStyle.Bold | FontStyle.Italic), GraphicsUnit.Pixel);
            Bitmap image = new Bitmap(50, 22);
            Graphics g = Graphics.FromImage(image);

            //设定图像大小
            SizeF stringSize = g.MeasureString(codeText, font);
            SizeF imageSize = new SizeF(stringSize.Width > imgStyle.ImgSize.Width ? stringSize.Width : imgStyle.ImgSize.Width,
                stringSize.Height > imgStyle.ImgSize.Height ? stringSize.Height : imgStyle.ImgSize.Height);

            image = new Bitmap((int)imageSize.Width, (int)imageSize.Height);
            g = Graphics.FromImage(image);

            //绘图设定
            g = Graphics.FromImage(image);
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;

            //绘制图像内容
            switch (imageType)
            {
                case ImageTypes.SimpleNoisePoint:
                    DrawNoisePoint(ref g, ref image, font, codeText, imgStyle);
                    break;
                case ImageTypes.SimpleNoiseLine:
                    DrawNoiseLine(ref g, ref image, font, codeText, imgStyle);
                    break;
                case ImageTypes.GridBackground:
                    DrawGrid(ref g, ref image, font, codeText, imgStyle);
                    break;
            }

            return image;
        }




        #region 绘制简单噪点验证码图像
        /// <summary>
        /// 绘制简单噪点验证码图像
        /// </summary>
        private static void DrawNoisePoint(ref Graphics g, ref Bitmap image, Font font, string codeText, ImageStyle imgStyle)
        {
            SizeF stringSize = g.MeasureString(codeText, font);

            //生成随机生成器
            Random random = new Random();

            //清空图片背景色
            g.Clear(imgStyle.ImgBgColor);

            //画图片的背景噪音线
            for (int i = 0; i < 25; i++)
            {
                int x1 = random.Next(image.Width);
                int x2 = random.Next(image.Width);
                int y1 = random.Next(image.Height);
                int y2 = random.Next(image.Height);

                g.DrawLine(new Pen(imgStyle.ImgNoiseColor), x1, y1, x2, y2);
            }

            //画文字
            System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, (int)stringSize.Width, (int)stringSize.Height), imgStyle.TextColor1, imgStyle.TextColor2, 1.2f, true);
            g.DrawString(codeText, font, brush, (image.Width - stringSize.Width) / 2, (image.Height - stringSize.Height) / 2);

            //画图片的前景噪音点
            for (int i = 0; i < 80; i++)
            {
                int x = random.Next(image.Width);
                int y = random.Next(image.Height);

                image.SetPixel(x, y, Color.FromArgb(random.Next()));
            }

            //画图片的边框线
            g.DrawRectangle(new Pen(imgStyle.ImgBorderColor), 0, 0, image.Width - 1, image.Height - 1);

        }
        #endregion

        #region 绘制简单干扰线验证码图像
        /// <summary>
        /// 绘制简单干扰线验证码图像
        /// </summary>
        internal static void DrawNoiseLine(ref Graphics g, ref Bitmap image, Font font, string codeText, ImageStyle imgStyle)
        {
            SizeF stringSize = g.MeasureString(codeText, font);

            //生成随机生成器
            Random random = new Random();

            //清空图片背景色
            g.Clear(imgStyle.ImgBgColor);

            //画文字
            LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), imgStyle.TextColor1, imgStyle.TextColor2, 1.2f, true);
            g.DrawString(codeText, font, brush, (image.Width - stringSize.Width) / 2, (image.Height - stringSize.Height) / 2);

            //画图片的噪音线
            int hp = (int)(image.Height / 4);
            int x1 = random.Next(0, image.Width / 8 * 1);
            int x2 = random.Next(image.Width / 4 * 1, image.Width / 4 * 2);
            int x3 = random.Next(image.Width / 4 * 2, image.Width / 4 * 3);
            int x4 = random.Next(image.Width / 8 * 7, image.Width);
            int y1 = random.Next(hp, hp * 3);
            int y2 = random.Next(0, hp * 4);
            int y3 = random.Next(0, hp * 4);
            int y4 = random.Next(hp, hp * 3);

            int lineWidth = imgStyle.TextFontSize / 10 + 1;
            g.DrawBezier(new Pen(imgStyle.ImgNoiseColor, lineWidth), x1, y1, x2, y2, x3, y3, x4, y4);

            //画图片的边框线
            g.DrawRectangle(new Pen(imgStyle.ImgBorderColor), 0, 0, image.Width - 1, image.Height - 1);
        }
        #endregion

        #region 绘制网格背景图像
        /// <summary>
        /// 绘制网格背景图像
        /// </summary>
        private static void DrawGrid(ref Graphics g, ref Bitmap image, Font font, string codeText, ImageStyle imgStyle)
        {
            SizeF stringSize = g.MeasureString(codeText, font);

            //生成随机生成器
            Random random = new Random();

            //清空图片背景色
            g.Clear(imgStyle.ImgBgColor);

            //画背景网格
            Pen pen = new Pen(imgStyle.ImgNoiseColor, 1);
            for (int i = 0; i < image.Width; i += 2)
            {
                g.DrawLine(pen, i + 1, 0, i + 1, image.Height);
            }
            for (int i = 0; i < image.Height; i += 2)
            {
                g.DrawLine(pen, 0, i + 1, image.Width, i + 1);
            }

            //画文字
            SolidBrush brush = new SolidBrush(imgStyle.TextColor1);
            int currentPos = (int)((image.Width - stringSize.Width) / 2);
            for (int i = 0; i < codeText.Length; i++)
            {
                SizeF charSize = g.MeasureString(codeText[i].ToString(), font);
                float sx = random.Next(800, 1000) / 1000f;
                float sy = random.Next(700, 1000) / 1000f;
                charSize.Width *= sx;
                charSize.Height *= sy;
                int y = random.Next(0, (int)((image.Height - charSize.Height*0.7)) / 2);
                float r = random.Next(-100, 100) / 10;

                g.TranslateTransform(currentPos, y);
                g.ScaleTransform(sx, sy);
                g.RotateTransform(r);
                g.DrawString(codeText[i].ToString(), font, brush, 0, 0);
                g.ResetTransform();
                currentPos += (int)(charSize.Width * 0.7);
            }

            //画图片的边框线
            g.DrawRectangle(new Pen(imgStyle.ImgBorderColor), 0, 0, image.Width - 1, image.Height - 1);
        }
        #endregion
    }
}
